<template>
  <div class="mod-excel">
    <el-upload
      class="upload-demo"
      action=""
      :on-change="handleChange"
      :on-remove="handleRemove"
      :on-exceed="handleExceed"
      :limit="limitUpload"
      accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
      :auto-upload="false"
    >
      <el-button v-if="isAuth('sys:excel:update')" size="big" type="primary"
        >上传</el-button
      >
    </el-upload>
    <el-form>
      <el-button
        type="primary"
        :disabled="isExcu"
        @click="dataFormSubmit"
        size="big"
        >提交</el-button
      >
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExcu: true,
      limitUpload: 1,
      fileTemp: null,
      file: null,
      dataFrom: {},
      rList: [],
      hList: [],
    };
  },

  methods: {
    //上传文件时处理方法
    handleChange(file, fileList) {
      this.fileTemp = file.raw;
      this.importfxx(this.fileTemp);
      this.isExcu = false;
    },
    //超出最大上传文件数量时的处理方法
    handleExceed() {
      this.$message({
        type: "warning",
        message: "超出最大上传文件数量的限制！",
      });
      return;
    },
    //移除文件的操作方法
    handleRemove(file, fileList) {
      this.fileTemp = null;
    },
    // 读取excel方法
    importfxx(obj) {
      let _this = this;
      let inputDOM = this.$refs.inputer;
      // 通过DOM取文件数据

      this.file = event.currentTarget.files[0];

      var rABS = false; //是否将文件读取为二进制字符串
      var f = this.file;

      var reader = new FileReader();
      //if (!FileReader.prototype.readAsBinaryString) {
      FileReader.prototype.readAsBinaryString = function (f) {
        var binary = "";
        var rABS = false; //是否将文件读取为二进制字符串
        var pt = this;
        var wb; //读取完成的数据
        var outdata;
        var reader = new FileReader();
        reader.onload = function (e) {
          var bytes = new Uint8Array(reader.result);
          var length = bytes.byteLength;
          for (var i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          //此处引入，用于解析excel
          var XLSX = require("xlsx");
          if (rABS) {
            wb = XLSX.read(btoa(fixdata(binary)), {
              //手动转化
              type: "base64",
            });
          } else {
            wb = XLSX.read(binary, {
              type: "binary",
            });
          }
          outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
          //outdata就是读取的数据（不包含标题行即表头，表头会作为对象的下标）
          //此处可对数据进行处理
          let roomArr = [];
          let hotelArr = [];
          let hotelList = [];
          outdata.map((v) => {
            let roomObj = {};
            let hotelObj = {};
            let rname = v["飞猪房型名称"];
            if (rname != undefined & rname != "") {
              roomObj.id = v["美团房型id"];
              roomObj.roomName = rname;
              roomObj.isCorrect = 1;
              roomArr.push(roomObj);
            }
            let htlId = v["美团酒店id"];
            let hname = v["飞猪酒店名称"];
            let addr = v["飞猪地址"];
            let p = v["飞猪电话"]
            let sid = v["飞猪shid"]
            if (!hotelList.includes(htlId) && hname != (undefined && "")) {
              hotelList.push(htlId);
              hotelObj.id = htlId;
              hotelObj.manualVerify = 1;
              hotelObj.hotelName = hname;
              hotelObj.address = addr;
              hotelObj.phone = p;
              hotelObj.shid = sid;
              hotelArr.push(hotelObj);
            }
          });
          console.info(roomArr);
          _this.rList = roomArr;
          _this.hList = hotelArr;
        };
        reader.readAsArrayBuffer(f);
      };
      if (rABS) {
        reader.readAsArrayBuffer(f);
      } else {
        reader.readAsBinaryString(f);
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.isExcu = true;
      this.$http({
        url: this.$http.adornUrl("/sys/excel/update"),
        method: "post",
        data: this.$http.adornData({
          htls: this.hList,
          rooms: this.rList,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: data.msg,
            type: "success",
            duration: 1500,
          });
        } else {
          this.$message.error(data.msg);
          this.isExcu = false;
        }
        this.isExcu = true;
        this.fileTemp = null;
      });
    },
  },
};
</script>
